package com.drug.service.impl;

import com.drug.domain.*;
import com.drug.mapper.*;
import com.drug.service.DeliveryService;
import com.drug.util.PinyinUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 供货管理
 */
@Service
public class DeliveryServiceImpl implements DeliveryService {
    @Autowired
    private PodiPdMapper podiPdMapper;
    @Autowired
    private PurchaseOrderMapper purchaseOrderMapper;
    @Autowired
    private HospitalMapper hospitalMapper;
    @Autowired
    private DrugInformationMapper drugInformationMapper;
    @Autowired
    private DrugAtegoryMapper drugAtegoryMapper;
    @Autowired
    private ManufacturerMapper manufacturerMapper;
    @Override
    public Result findPurchaseOrder(Map<String, Object> map) {
        //分页参数
        String pageNum = (String) map.get("pageNum");
        if (pageNum!=null){
            PageHelper.startPage(Integer.parseInt(pageNum),5);
        }
        //使用分页插件
        PurchaseOrder purchaseOrder = new PurchaseOrder();
        DrugInformation drugInformation =new DrugInformation();
        DrugAtegory drugAtegory = new DrugAtegory();
        DrugCategory drugCategory = new DrugCategory();
        Manufacturer manufacturer = new Manufacturer();
        Hospital hospital = new Hospital();
        //采购单编号
        String pnumber = (String) map.get("pnumber");
        if (pnumber==null||pnumber.equals("")){
            pnumber=null;
        }else {
            purchaseOrder.setPoNumber(pnumber);
        }
        //采购单名称
        String pname = (String) map.get("pname");
        if (pname==null||pname.equals("")){
            pname=null;
        }else {
            purchaseOrder.setPoName(pname);
        }
        //采购状态
        String status = (String) map.get("status");
        if (status==null||status.equals("")||status.equals("0")){
            status=null;
        }else {
            purchaseOrder.setState(status);
        }
        //采购医院
        String ph = (String) map.get("ph");
        if (ph==null||ph.equals("")){
            ph=null;
        }else {
            hospital.sethName(ph);
        }
        //采购时间
        String lefTime=null;
        if (lefTime==null||lefTime.equals("")){
        }else {
            lefTime = (String) map.get("lefTime");
        }

        //采购时间
        String rightTime=null;
        if (rightTime==null||rightTime.equals("")){
        }else {
            rightTime = (String) map.get("rightTime");
        }
        //药品流水号
        String dserial = (String) map.get("dserial");
        if (dserial==null||dserial.equals("")){
            dserial=null;
        }else {
            drugInformation.setDiNumer(dserial);
        }
        //通用名
        String cname = (String) map.get("cname");
        if (cname==null||cname.equals("")){
            cname=null;
        }else {
            //获取所有的通用名
            String[] strings = drugInformationMapper.selectAllName();
            for (int i = 0; i < strings.length; i++) {
                String pinYinHeadChar = PinyinUtils.getPinYinHeadChar(strings[i]);
                String substring = pinYinHeadChar.substring(0,1);
                if (substring.equals(cname)){
                    drugInformation.setDiName(strings[i]);
                }
            }
        }
        //剂型
        String dform = (String) map.get("dform");
        if (dform==null||dform.equals("")){
            dform=null;
        }else {
            drugInformation.setDiForm(dform);
        }
        //规格
        String specification = (String) map.get("specification");
        if (specification==null||specification.equals("")){
            specification=null;
        }else {
            drugInformation.setDiSpecifications(specification);
        }
        //单位
        String unit = (String) map.get("unit");
        if (unit==null||unit.equals("")){
            unit=null;
        }else {
            drugInformation.setDiUnit(unit);
        }
        //转换系数
        String cc = (String) map.get("cc");
        if (cc==null||cc.equals("")){
            cc=null;
        }else {
            drugInformation.setDiCc(cc);
        }
        //生产企业
        String manufacture = (String) map.get("manufacturer");
        if (manufacture==null||manufacture.equals("")){
            manufacture=null;
        }else {
            manufacturer.setMfName(manufacture);
        }
        //商品名
        String pdname= (String) map.get("pdname");
        if (pdname==null||pdname.equals("")){
            pdname=null;
        }else {
            drugAtegory.setDaName(pdname);
        }
        //药品类别
        String dcid = (String) map.get("dcid");
        if (dcid==null||dcid.equals("")||dcid.equals("0")){
            dcid=null;
        }else {
            drugCategory.setDcName(dcid);
        }

        PodiPd podiPd = new PodiPd();
        podiPd.setDrugAtegory(drugAtegory);
        podiPd.setDrugCategory(drugCategory);
        podiPd.setDrugInformation(drugInformation);
        podiPd.setHospital(hospital);
        podiPd.setPurchaseOrder(purchaseOrder);
        podiPd.setManufacturer(manufacturer);
        List<PodiPd> podiPdList = podiPdMapper.selectPodiPd(podiPd, lefTime, rightTime);
        PageInfo<PodiPd> pageInfo= new PageInfo<PodiPd>(podiPdList);
        Result result = new Result();
        result.setData(pageInfo);
        return result;
    }

    @Override
    public List<PodiPd> findAllPo() {
        return podiPdMapper.selectAllPo();
    }

    /**
     * 导入表格
     * @param goods
     */
    @Override
    public Result InsertInfo(Goods goods) {
        Result result = new Result();
        String poNumber = goods.getPurchaseOrder();
        String hospital = goods.getHospital();
        Integer hId= hospitalMapper.selectIdByName(hospital);
        PurchaseOrder purchaseOrder = new PurchaseOrder();
        purchaseOrder.setPoNumber(poNumber);
        purchaseOrder.setPoUId(hId);
        purchaseOrder.setPoState("0");
        int i = purchaseOrderMapper.insetImp(purchaseOrder);
        Integer poId=0;
        if (i==1){
            //采购单id
            poId = purchaseOrder.getPoId();
        }
        DrugInformation drugInformation = new DrugInformation();
        String diNumer = goods.getDiNumer();
        String diName = goods.getDiName();
        String daName = goods.getDaName();
        String diForm = goods.getDiForm();
        String diSpecifications = goods.getDiSpecifications();
        String diUnit = goods.getDiUnit();
        String diCc = goods.getDiCc();
        String manufacturer = goods.getManufacturer();
        drugInformation.setDiNumer(diNumer);
        drugInformation.setDiName(diName);
        drugInformation.setDiForm(diForm);
        drugInformation.setDiSpecifications(diSpecifications);
        drugInformation.setDiUnit(diUnit);
        drugInformation.setDiCc(diCc);
        Integer a=drugInformationMapper.insetImp(drugInformation);
        Integer diId=0;
        if (a==1){
            //药品信息id
             diId = drugInformation.getDiId();
        }
        DrugAtegory drugAtegory = new DrugAtegory();
        drugAtegory.setDaName(daName);
        Integer integer = manufacturerMapper.selectIdByName(manufacturer);
        drugAtegory.setDaMName(integer+"");
        Integer bb=drugAtegoryMapper.insertImp(drugAtegory);
        Integer daId=0;
        if (bb==1){
            //药品目录id
             daId= drugAtegory.getDaId();
             drugInformation.setDiDaCategory(daId);
        }
        PodiPd podiPd = new PodiPd();
        podiPd.setPodiDiid(diId);
        podiPd.setPodiHid(hId);
        podiPd.setPodiPoid(poId);
        podiPdMapper.insertImp( podiPd);
        return result;
    }

    /**
     * 选择发货
     * @param id
     * @return
     */
    @Override
    public Result reviseState(Integer[] id) {
        Result result = new Result();
        if (id!=null){
            int aa =0;
            int cc =0;
            for (int i = 0; i < id.length; i++) {
                cc = podiPdMapper.updateState(id[i]);
               int bb= podiPdMapper.selectPoId(id[i]);
                aa=purchaseOrderMapper.updateState(bb);
                aa++;
            }
        int b =id.length-aa;
        result.setMsg("您的操作成功!成功"+aa+"条,失败"+b+"条!");
    }else {
        result.setMsg("请选择!!!");
    }
        return result;
    }
}
